package org.jeecg.modules.online.cgform.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.lang.invoke.SerializedLambda;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.query.MatchTypeEnum;
import org.jeecg.common.system.util.JeecgDataAutorUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.system.vo.SysPermissionDataRuleModel;
import org.jeecg.common.util.dynamic.db.DbTypeUtils;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.online.auth.service.IOnlAuthDataService;
import org.jeecg.modules.online.cgform.consts.ExtendJsonKey;
import org.jeecg.modules.online.cgform.consts.OnlineConst;
import org.jeecg.modules.online.cgform.converter.ConvertUtil;
import org.jeecg.modules.online.cgform.entity.OnlCgformField;
import org.jeecg.modules.online.cgform.entity.OnlCgformHead;
import org.jeecg.modules.online.cgform.mapper.OnlineMapper;
import org.jeecg.modules.online.cgform.model.OnlQueryModel;
import org.jeecg.modules.online.cgform.model.OnlTable;
import org.jeecg.modules.online.cgform.model.SqlOrder;
import org.jeecg.modules.online.cgform.service.IOnlCgformFieldService;
import org.jeecg.modules.online.cgform.service.IOnlCgformHeadService;
import org.jeecg.modules.online.cgform.service.IOnlineJoinQueryService;
import org.jeecg.modules.online.cgform.utils.CgformUtil;
import org.jeecg.modules.online.config.exception.BusinessException;
import org.jeecg.modules.online.config.p020b.DataBaseConfig;
import org.jeecg.modules.online.config.p020b.OnlineFieldConfig;
import org.jeecg.modules.online.config.p022d.DbTableUtil;
import org.jeecg.modules.online.p002a.ConditionHandler;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.enmus.ExcelType;
import org.jeecgframework.poi.excel.entity.params.ExcelExportEntity;
import org.jeecgframework.poi.excel.export.ExcelExportServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

@Service("onlineJoinQueryService")
/* renamed from: org.jeecg.modules.online.cgform.service.a.i */
/* loaded from: hibernate-re-3.7.1-RC.jar:org/jeecg/modules/online/cgform/service/a/i.class */
public class OnlineJoinQueryServiceImpl implements IOnlineJoinQueryService {

    /* renamed from: a */
    private static final Logger f462a = LoggerFactory.getLogger(OnlineJoinQueryServiceImpl.class);
    @Autowired
    IOnlCgformFieldService onlCgformFieldService;
    @Autowired
    IOnlCgformHeadService onlCgformHeadService;
    @Autowired
    private IOnlAuthDataService onlAuthDataService;
    @Autowired
    @Lazy
    private ISysBaseAPI sysBaseAPI;
    @Autowired
    private OnlineMapper onlineMapper;
    @Value("${jeecg.path.upload}")
    private String upLoadPath;

    /* renamed from: a */
//    private static /* synthetic */ Object m168a(SerializedLambda serializedLambda) {
//        String implMethodName = serializedLambda.getImplMethodName();
//        boolean z = true;
//        switch (implMethodName.hashCode()) {
//            case -1998322493:
//                if (implMethodName.equals("getTableName")) {
//                    z = true;
//                    break;
//                }
//                break;
//            case -1279489738:
//                if (implMethodName.equals("getDbIsPersist")) {
//                    z = true;
//                    break;
//                }
//                break;
//            case -982778162:
//                if (implMethodName.equals("getOrderNum")) {
//                    z = false;
//                    break;
//                }
//                break;
//            case 713008857:
//                if (implMethodName.equals("getCgformHeadId")) {
//                    z = true;
//                    break;
//                }
//                break;
//        }
//        switch (z) {
//            case false:
//                if (serializedLambda.getImplMethodKind() == 5 && serializedLambda.getFunctionalInterfaceClass().equals("com/baomidou/mybatisplus/core/toolkit/support/SFunction") && serializedLambda.getFunctionalInterfaceMethodName().equals("apply") && serializedLambda.getFunctionalInterfaceMethodSignature().equals("(Ljava/lang/Object;)Ljava/lang/Object;") && serializedLambda.getImplClass().equals("org/jeecg/modules/online/cgform/entity/OnlCgformField") && serializedLambda.getImplMethodSignature().equals("()Ljava/lang/Integer;")) {
//                    return (v0) -> {
//                        return v0.getOrderNum();
//                    };
//                }
//                if (serializedLambda.getImplMethodKind() == 5 && serializedLambda.getFunctionalInterfaceClass().equals("com/baomidou/mybatisplus/core/toolkit/support/SFunction") && serializedLambda.getFunctionalInterfaceMethodName().equals("apply") && serializedLambda.getFunctionalInterfaceMethodSignature().equals("(Ljava/lang/Object;)Ljava/lang/Object;") && serializedLambda.getImplClass().equals("org/jeecg/modules/online/cgform/entity/OnlCgformField") && serializedLambda.getImplMethodSignature().equals("()Ljava/lang/Integer;")) {
//                    return (v0) -> {
//                        return v0.getOrderNum();
//                    };
//                }
//                break;
//            case true:
//                if (serializedLambda.getImplMethodKind() == 5 && serializedLambda.getFunctionalInterfaceClass().equals("com/baomidou/mybatisplus/core/toolkit/support/SFunction") && serializedLambda.getFunctionalInterfaceMethodName().equals("apply") && serializedLambda.getFunctionalInterfaceMethodSignature().equals("(Ljava/lang/Object;)Ljava/lang/Object;") && serializedLambda.getImplClass().equals("org/jeecg/modules/online/cgform/entity/OnlCgformField") && serializedLambda.getImplMethodSignature().equals("()Ljava/lang/String;")) {
//                    return (v0) -> {
//                        return v0.getCgformHeadId();
//                    };
//                }
//                if (serializedLambda.getImplMethodKind() == 5 && serializedLambda.getFunctionalInterfaceClass().equals("com/baomidou/mybatisplus/core/toolkit/support/SFunction") && serializedLambda.getFunctionalInterfaceMethodName().equals("apply") && serializedLambda.getFunctionalInterfaceMethodSignature().equals("(Ljava/lang/Object;)Ljava/lang/Object;") && serializedLambda.getImplClass().equals("org/jeecg/modules/online/cgform/entity/OnlCgformField") && serializedLambda.getImplMethodSignature().equals("()Ljava/lang/String;")) {
//                    return (v0) -> {
//                        return v0.getCgformHeadId();
//                    };
//                }
//                break;
//            case true:
//                if (serializedLambda.getImplMethodKind() == 5 && serializedLambda.getFunctionalInterfaceClass().equals("com/baomidou/mybatisplus/core/toolkit/support/SFunction") && serializedLambda.getFunctionalInterfaceMethodName().equals("apply") && serializedLambda.getFunctionalInterfaceMethodSignature().equals("(Ljava/lang/Object;)Ljava/lang/Object;") && serializedLambda.getImplClass().equals("org/jeecg/modules/online/cgform/entity/OnlCgformHead") && serializedLambda.getImplMethodSignature().equals("()Ljava/lang/String;")) {
//                    return (v0) -> {
//                        return v0.getTableName();
//                    };
//                }
//                if (serializedLambda.getImplMethodKind() == 5 && serializedLambda.getFunctionalInterfaceClass().equals("com/baomidou/mybatisplus/core/toolkit/support/SFunction") && serializedLambda.getFunctionalInterfaceMethodName().equals("apply") && serializedLambda.getFunctionalInterfaceMethodSignature().equals("(Ljava/lang/Object;)Ljava/lang/Object;") && serializedLambda.getImplClass().equals("org/jeecg/modules/online/cgform/entity/OnlCgformHead") && serializedLambda.getImplMethodSignature().equals("()Ljava/lang/String;")) {
//                    return (v0) -> {
//                        return v0.getTableName();
//                    };
//                }
//                if (serializedLambda.getImplMethodKind() == 5 && serializedLambda.getFunctionalInterfaceClass().equals("com/baomidou/mybatisplus/core/toolkit/support/SFunction") && serializedLambda.getFunctionalInterfaceMethodName().equals("apply") && serializedLambda.getFunctionalInterfaceMethodSignature().equals("(Ljava/lang/Object;)Ljava/lang/Object;") && serializedLambda.getImplClass().equals("org/jeecg/modules/online/cgform/entity/OnlCgformHead") && serializedLambda.getImplMethodSignature().equals("()Ljava/lang/String;")) {
//                    return (v0) -> {
//                        return v0.getTableName();
//                    };
//                }
//                break;
//            case true:
//                if (serializedLambda.getImplMethodKind() == 5 && serializedLambda.getFunctionalInterfaceClass().equals("com/baomidou/mybatisplus/core/toolkit/support/SFunction") && serializedLambda.getFunctionalInterfaceMethodName().equals("apply") && serializedLambda.getFunctionalInterfaceMethodSignature().equals("(Ljava/lang/Object;)Ljava/lang/Object;") && serializedLambda.getImplClass().equals("org/jeecg/modules/online/cgform/entity/OnlCgformField") && serializedLambda.getImplMethodSignature().equals("()Ljava/lang/Integer;")) {
//                    return (v0) -> {
//                        return v0.getDbIsPersist();
//                    };
//                }
//                break;
//        }
//        throw new IllegalArgumentException("Invalid lambda deserialization");
//    }

    @Override // org.jeecg.modules.online.cgform.service.IOnlineJoinQueryService
    public Map<String, Object> pageList(OnlCgformHead head, Map<String, Object> params, boolean ignoreSelectSubField) {
        OnlQueryModel queryInfo = getQueryInfo(head, params, ignoreSelectSubField);
        String sql = queryInfo.getSql();
        Map<String, Object> params2 = queryInfo.getParams();
        Map<String, String> tableAliasMap = queryInfo.getTableAliasMap();
        HashMap hashMap = new HashMap(5);
        Integer valueOf = Integer.valueOf(params.get("pageSize") == null ? 10 : Integer.parseInt(params.get("pageSize").toString()));
        if (valueOf.intValue() == -521) {
            List<Map<String, Object>> selectByCondition = this.onlineMapper.selectByCondition(sql, params2);
            if (selectByCondition == null || selectByCondition.size() == 0) {
                hashMap.put("total", 0);
            } else {
                hashMap.put("total", Integer.valueOf(selectByCondition.size()));
                if (ignoreSelectSubField) {
                    selectByCondition = m155b(selectByCondition);
                }
                hashMap.put("records", CgformUtil.m364a(selectByCondition, tableAliasMap.values()));
            }
            if (ignoreSelectSubField) {
                hashMap.put("fieldList", queryInfo.getFieldList());
            }
        } else {
            Page<Map<String, Object>> page = new Page<>(Integer.valueOf(params.get("pageNo") == null ? 1 : Integer.parseInt(params.get("pageNo").toString())).intValue(), valueOf.intValue());
            page.setOptimizeCountSql(false);
            IPage<Map<String, Object>> selectPageByCondition = this.onlineMapper.selectPageByCondition(page, sql, params2);
            hashMap.put("total", Long.valueOf(selectPageByCondition.getTotal()));
            List<Map<String, Object>> records = selectPageByCondition.getRecords();
            if (ignoreSelectSubField) {
                records = m155b(records);
            }
            hashMap.put("records", CgformUtil.m364a(records, tableAliasMap.values()));
        }
        return hashMap;
    }

    /* renamed from: a */
    private String m156a(OnlTable onlTable, String str, String str2, String str3) {
        String alias = onlTable.getAlias();
        String m317f = CgformUtil.m317f(onlTable.getTableName());
        String str4 = alias + ".";
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(" AND EXISTS (");
        stringBuffer.append(CgformUtil.SELECT);
        stringBuffer.append(str4 + "id");
        stringBuffer.append(CgformUtil.f204b);
        stringBuffer.append(m317f);
        stringBuffer.append(" " + alias);
        stringBuffer.append(CgformUtil.f211i);
        stringBuffer.append(str4);
        stringBuffer.append(onlTable.getJoinField());
        stringBuffer.append(CgformUtil.f215m);
        stringBuffer.append(str);
        stringBuffer.append(onlTable.getMainField());
        if (str2 != null && str2.length() > 0) {
            stringBuffer.append(str2);
        }
        if (str3 != null && str3.length() > 0) {
            stringBuffer.append(" AND (").append(str3).append(") ");
        }
        stringBuffer.append(")");
        return stringBuffer.toString();
    }

    @Override // org.jeecg.modules.online.cgform.service.IOnlineJoinQueryService
    public Map<String, Object> pageList(OnlCgformHead head, Map<String, Object> params) {
        return pageList(head, params, false);
    }

    /* renamed from: a */
    private String m165a(List<String> list, Map<String, Integer> map, Map<String, String> map2) {
        ArrayList arrayList = new ArrayList();
        for (String str : list) {
            String[] split = str.split("\\.");
            String str2 = split[0];
            if ("a".equals(str2)) {
                arrayList.add(str);
            } else {
                String str3 = split[1];
                if (map.get(str3).intValue() > 1) {
                    arrayList.add(str + " " + CgformUtil.m308l(map2.get(str2)) + "_" + str3);
                } else {
                    arrayList.add(str);
                }
            }
        }
        return String.join(CgformUtil.f233E, arrayList);
    }

    /* renamed from: a */
    private void m171a(String str, boolean z, List<OnlCgformField> list, List<String> list2, Map<String, Integer> map) {
        if (list == null || list.size() == 0) {
            if (z) {
                list2.add(str + "id");
                return;
            }
            return;
        }
        int size = list.size();
        for (int i = 0; i < size; i++) {
            OnlCgformField onlCgformField = list.get(i);
            String dbFieldName = onlCgformField.getDbFieldName();
            if (!"id".equals(dbFieldName) && 1 == onlCgformField.getIsShowList().intValue()) {
                if (CgformUtil.f247S.equals(onlCgformField.getFieldShowType()) && oConvertUtils.isNotEmpty(onlCgformField.getDictText())) {
                    list2.add(str + onlCgformField.getDictText());
                }
                list2.add(str + dbFieldName);
                Integer num = map.get(dbFieldName);
                if (num == null) {
                    map.put(dbFieldName, 1);
                } else {
                    map.put(dbFieldName, Integer.valueOf(1 + num.intValue()));
                }
            }
        }
        list2.add(str + "id");
        map.put("id", 2);
    }

    /* renamed from: a */
    private OnlTable m162a(OnlCgformHead onlCgformHead, int i, boolean z) {
        String id = onlCgformHead.getId();
        String tableName = onlCgformHead.getTableName();
        OnlTable onlTable = new OnlTable(tableName, id, z);
        List<OnlCgformField> m172a = m172a(id);
        List<OnlCgformField> queryAvailableFields = this.onlCgformFieldService.queryAvailableFields(id, tableName, true, m172a, null);
        onlTable.setAllFieldList(m172a);
        onlTable.setSelectFieldList(queryAvailableFields);
        onlTable.setAliasByIntValue(i);
        if (!z) {
            Iterator<OnlCgformField> it = m172a.iterator();
            while (true) {
                if (!it.hasNext()) {
                    break;
                }
                OnlCgformField next = it.next();
                if (oConvertUtils.isNotEmpty(next.getMainField()) && oConvertUtils.isNotEmpty(next.getMainTable())) {
                    onlTable.setMainField(next.getMainField());
                    onlTable.setJoinField(next.getDbFieldName());
                    break;
                }
            }
        }
        return onlTable;
    }

    /* renamed from: a */
    private List<OnlTable> m161a(OnlCgformHead onlCgformHead, String str) {
        String subTableStr;
        ArrayList arrayList = new ArrayList();
        int i = 97 + 1;
        OnlTable m162a = m162a(onlCgformHead, 97, true);
        m162a.setAuthList(this.onlAuthDataService.queryUserOnlineAuthData(str, onlCgformHead.getId()));
        arrayList.add(m162a);
        Integer tableType = onlCgformHead.getTableType();
        if (tableType != null && tableType.intValue() == 2 && (subTableStr = onlCgformHead.getSubTableStr()) != null && !"".equals(subTableStr)) {
            for (String str2 : subTableStr.split(CgformUtil.f233E)) {
                OnlCgformHead onlCgformHead2 = this.onlCgformHeadService.getOne( new LambdaQueryWrapper<OnlCgformHead>().eq(OnlCgformHead::getTableName, str2));
                if (onlCgformHead2 != null) {
                    int i2 = i;
                    i++;
                    OnlTable m162a2 = m162a(onlCgformHead2, i2, false);
                    m162a2.setAuthList(this.onlAuthDataService.queryUserOnlineAuthData(str, onlCgformHead2.getId()));
                    arrayList.add(m162a2);
                }
            }
        }
        return arrayList;
    }

    /* renamed from: a */
    private Map<String, List<OnlCgformField>> m160a(OnlCgformHead onlCgformHead, Map<String, String> map) {
        String subTableStr;
        HashMap hashMap = new HashMap(5);
        map.put(onlCgformHead.getTableName(), onlCgformHead.getId());
        hashMap.put(onlCgformHead.getTableName(), m172a(onlCgformHead.getId()));
        Integer tableType = onlCgformHead.getTableType();
        if (tableType != null && tableType.intValue() == 2 && (subTableStr = onlCgformHead.getSubTableStr()) != null && !"".equals(subTableStr)) {
            for (String str : subTableStr.split(CgformUtil.f233E)) {
                OnlCgformHead onlCgformHead2 = this.onlCgformHeadService.getOne( new LambdaQueryWrapper<OnlCgformHead>().eq(OnlCgformHead::getTableName, str));
                if (onlCgformHead2 != null) {
                    map.put(onlCgformHead2.getTableName(), onlCgformHead2.getId());
                    hashMap.put(onlCgformHead2.getTableName(), m172a(onlCgformHead2.getId()));
                }
            }
        }
        return hashMap;
    }

    /* renamed from: a */
    private List<OnlCgformField> m172a(String str) {
    	LambdaQueryWrapper<OnlCgformField> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(OnlCgformField::getCgformHeadId, str);
        lambdaQueryWrapper.eq(OnlCgformField::getDbIsPersist, OnlineConst.f140b);
        lambdaQueryWrapper.orderByAsc(OnlCgformField::getOrderNum);
        return this.onlCgformFieldService.list(lambdaQueryWrapper);
    }

    /* renamed from: a */
    private boolean m163a(Map<String, Object> map, boolean z, String str, String str2, List<OnlCgformField> list, List<SqlOrder> list2) {
        String string;
        boolean z2 = z;
        Object obj = map.get("column");
        if (obj != null && !"id".equals(obj.toString())) {
            String obj2 = obj.toString();
            Object obj3 = map.get("order");
            String str3 = "desc";
            if (obj3 != null) {
                str3 = obj3.toString();
            }
            if (z) {
                if (CgformUtil.m331c(obj2, list)) {
                    SqlOrder sqlOrder = new SqlOrder(obj2, str3);
                    sqlOrder.setAlias(str2);
                    list2.add(sqlOrder);
                }
            } else if (obj2.startsWith(str)) {
                String replaceFirst = obj2.replaceFirst(str + "_", "");
                if (CgformUtil.m331c(replaceFirst, list)) {
                    SqlOrder sqlOrder2 = new SqlOrder(replaceFirst, str3);
                    sqlOrder2.setAlias(str2);
                    list2.add(sqlOrder2);
                    z2 = true;
                }
            }
        } else {
            for (OnlCgformField onlCgformField : list) {
                if ("1".equals(onlCgformField.getSortFlag())) {
                    String fieldExtendJson = onlCgformField.getFieldExtendJson();
                    SqlOrder sqlOrder3 = new SqlOrder(onlCgformField.getDbFieldName());
                    sqlOrder3.setAlias(str2);
                    if (fieldExtendJson != null && !"".equals(fieldExtendJson) && (string = JSON.parseObject(fieldExtendJson).getString(ExtendJsonKey.f135h)) != null && !"".equals(string)) {
                        sqlOrder3.setRule(string);
                        list2.add(sqlOrder3);
                        z2 = true;
                    }
                }
            }
        }
        return z2;
    }

    /* renamed from: a */
    private String m167a(List<SqlOrder> list) {
        if (list.size() == 0) {
            list.add(SqlOrder.m262a("a."));
        }
        ArrayList arrayList = new ArrayList();
        for (SqlOrder sqlOrder : list) {
            arrayList.add(sqlOrder.getRealSql());
        }
        return " ORDER BY " + String.join(CgformUtil.f233E, arrayList);
    }

    /* renamed from: a */
    private String m170a(StringBuilder sb) {
        String sb2 = sb.toString();
        if (sb2 == null || "".equals(sb2)) {
            return "";
        }
        return " AND (" + sb2 + ") ";
    }

    /* renamed from: a */
    private boolean m169a(StringBuilder sb, JSONArray jSONArray, MatchTypeEnum matchTypeEnum, String str, String str2, List<OnlCgformField> list, boolean z, boolean z2) {
        boolean z3 = z2;
        if (jSONArray != null) {
            for (int i = 0; i < jSONArray.size(); i++) {
                JSONObject jSONObject = jSONArray.getJSONObject(i);
                String string = jSONObject.getString("field");
                String[] split = string.split(CgformUtil.f233E);
                if (split.length == 1) {
                    if (z && CgformUtil.m331c(string, list)) {
                        CgformUtil.m369a(sb, str + string, jSONObject, matchTypeEnum, null, z3);
                        z3 = false;
                    }
                } else {
                    String str3 = split[1];
                    if (str2.equalsIgnoreCase(split[0]) && CgformUtil.m331c(str3, list)) {
                        CgformUtil.m369a(sb, str + str3, jSONObject, matchTypeEnum, null, z3);
                        z3 = false;
                    }
                }
            }
        }
        return z3;
    }

    /* renamed from: b */
    private List<Map<String, Object>> m155b(List<Map<String, Object>> list) {
        HashMap hashMap = new HashMap(5);
        for (Map<String, Object> map : list) {
            String str = "";
            if (map.containsKey("id")) {
                str = map.get("id").toString();
            } else if (map.containsKey("ID")) {
                str = map.get("ID").toString();
            }
            hashMap.putIfAbsent(str, map);
        }
        return new ArrayList(hashMap.values());
    }

    /* renamed from: a */
    private boolean m158a(OnlTable onlTable, JSONArray jSONArray) {
        if (onlTable.m266a()) {
            return true;
        }
        String tableName = onlTable.getTableName();
        if (jSONArray != null && jSONArray.size() > 0) {
            for (int i = 0; i < jSONArray.size(); i++) {
                String[] split = jSONArray.getJSONObject(i).getString("field").split(CgformUtil.f233E);
                if (split.length == 2 && split[0] != null && split[0].equals(tableName)) {
                    return true;
                }
            }
            return false;
        }
        return false;
    }

    /* JADX WARN: Removed duplicated region for block: B:13:0x002b  */
    /* renamed from: a */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct code enable 'Show inconsistent code' option in preferences
    */
    private boolean m159a(org.jeecg.modules.online.cgform.model.OnlTable r4) {
        /*
            r3 = this;
            r0 = r4
            boolean r0 = r0.m266a()
            if (r0 == 0) goto L9
            r0 = 1
            return r0
        L9:
            r0 = r4
            java.util.List r0 = r0.getSelectFieldList()
            r5 = r0
            r0 = r5
            if (r0 == 0) goto L51
            r0 = r5
            int r0 = r0.size()
            if (r0 <= 0) goto L51
            r0 = r5
            java.util.Iterator r0 = r0.iterator()
            r6 = r0
        L22:
            r0 = r6
            boolean r0 = r0.hasNext()
            if (r0 == 0) goto L51
            r0 = r6
            java.lang.Object r0 = r0.next()
            org.jeecg.modules.online.cgform.entity.OnlCgformField r0 = (org.jeecg.modules.online.cgform.entity.OnlCgformField) r0
            r7 = r0
            r0 = r7
            java.lang.String r0 = r0.getMainTable()
            r8 = r0
            r0 = r8
            if (r0 == 0) goto L4c
            java.lang.String r0 = ""
            r1 = r8
            boolean r0 = r0.equals(r1)
            if (r0 == 0) goto L4e
        L4c:
            r0 = 1
            return r0
        L4e:
            goto L22
        L51:
            r0 = 0
            return r0
        */
        throw new UnsupportedOperationException("Method not decompiled: org.jeecg.modules.online.cgform.service.p014a.OnlineJoinQueryServiceImpl.m159a(org.jeecg.modules.online.cgform.model.f):boolean");
    }

    /* renamed from: a */
    private ConditionHandler m157a(OnlTable onlTable, JSONArray jSONArray, String str, ConditionHandler conditionHandler) {
        String tableName = onlTable.getTableName();
        boolean m266a = onlTable.m266a();
        List<OnlCgformField> allFieldList = onlTable.getAllFieldList();
        ArrayList arrayList = new ArrayList();
        if (jSONArray != null) {
            for (int i = 0; i < jSONArray.size(); i++) {
                JSONObject jSONObject = jSONArray.getJSONObject(i);
                String string = jSONObject.getString("field");
                String[] split = string.split(CgformUtil.f233E);
                if (split.length == 1) {
                    if (m266a && CgformUtil.m331c(string, allFieldList)) {
                        arrayList.add(new OnlineFieldConfig(jSONObject));
                    }
                } else {
                    String str2 = split[1];
                    if (tableName.equalsIgnoreCase(split[0]) && CgformUtil.m331c(str2, allFieldList)) {
                        arrayList.add(new OnlineFieldConfig(jSONObject));
                    }
                }
            }
            if (arrayList.size() > 0) {
                ConditionHandler conditionHandler2 = new ConditionHandler(onlTable.getAlias() + ".", true, str);
                conditionHandler2.setDuplicateSqlNameRecord(conditionHandler.getDuplicateSqlNameRecord());
                conditionHandler2.setDuplicateParamNameRecord(conditionHandler.getDuplicateParamNameRecord());
                conditionHandler2.m565a((List<OnlineFieldConfig>) arrayList);
                return conditionHandler2;
            }
            return null;
        }
        return null;
    }

    @Override // org.jeecg.modules.online.cgform.service.IOnlineJoinQueryService
    public OnlQueryModel getQueryInfo(OnlCgformHead head, Map<String, Object> params, boolean ignoreSelectSubField) {
        return getQueryInfo(head, params, ignoreSelectSubField, false);
    }

    @Override // org.jeecg.modules.online.cgform.service.IOnlineJoinQueryService
    public OnlQueryModel getQueryInfo(OnlCgformHead head, Map<String, Object> params, boolean ignoreSelectSubField, boolean isNewExport) {
        DbType m27c = DbTableUtil.m27c((DataBaseConfig) null);
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        List<OnlTable> m161a = m161a(head, loginUser.getId());
        JSONArray m336b = CgformUtil.m336b(params);
        MatchTypeEnum m327c = CgformUtil.m327c(params);
        StringBuilder sb = new StringBuilder();
        boolean z = true;
        String str = "";
        boolean z2 = false;
        StringBuffer stringBuffer = new StringBuffer();
        StringBuffer stringBuffer2 = new StringBuffer();
        ArrayList arrayList = new ArrayList();
        ArrayList arrayList2 = new ArrayList();
        Map<String, Integer> hashMap = new HashMap<>(5);
        HashMap hashMap2 = new HashMap(5);
        List arrayList3 = new ArrayList();
        HashMap hashMap3 = new HashMap(5);
        for (OnlTable onlTable : m161a) {
            List<OnlCgformField> selectFieldList = onlTable.getSelectFieldList();
            String alias = onlTable.getAlias();
            String str2 = alias + ".";
            String str3 = " " + alias + " ";
            String tableName = onlTable.getTableName();
            List<OnlCgformField> allFieldList = onlTable.getAllFieldList();
            List<SysPermissionDataRuleModel> authList = onlTable.getAuthList();
            if (!z2 && authList != null && authList.size() > 0) {
                JeecgDataAutorUtils.installUserInfo(this.sysBaseAPI.getCacheUser(loginUser.getUsername()));
                z2 = true;
            }
            ConditionHandler conditionHandler = new ConditionHandler(str2, m27c.getDb());
            conditionHandler.setTableName(tableName);
            conditionHandler.setNeedList(null);
            conditionHandler.setFirst(false);
            String m562a = conditionHandler.m562a(CgformUtil.m313g(allFieldList), params, authList, tableName + "@");
            hashMap3.putAll(conditionHandler.getSqlParams());
            boolean m163a = m163a(params, onlTable.m266a(), tableName, str2, allFieldList, arrayList2);
            boolean m159a = m159a(onlTable);
            boolean m158a = m158a(onlTable, m336b);
            boolean z3 = m562a.length() > 0;
            if (m159a || m158a || z3 || m163a) {
                boolean z4 = !m159a && (m158a || z3);
                if (m163a) {
                    z4 = false;
                }
                if ((ignoreSelectSubField && onlTable.m266a()) || (!ignoreSelectSubField && m159a)) {
                    m171a(str2, onlTable.m266a(), selectFieldList, arrayList, hashMap);
                }
                String str4 = "";
                ConditionHandler m157a = m157a(onlTable, m336b, m327c.getValue(), conditionHandler);
                if (m157a != null) {
                    str4 = m157a.getSql().toString();
                    if (str4.length() > 0) {
                        hashMap3.putAll(m157a.getSqlParams());
                    }
                }
                if (onlTable.m266a()) {
                    stringBuffer.append(CgformUtil.f204b + CgformUtil.m317f(tableName) + str3);
                    str = str2;
                } else {
                    hashMap2.put(alias, tableName);
                    if (z4) {
                        stringBuffer2.append(m156a(onlTable, str, m562a, str4));
                    } else {
                        stringBuffer.append(" LEFT JOIN ");
                        stringBuffer.append(CgformUtil.m317f(tableName));
                        stringBuffer.append(str3);
                        stringBuffer.append(CgformUtil.f206d);
                        stringBuffer.append(str2);
                        stringBuffer.append(onlTable.getJoinField());
                        stringBuffer.append(CgformUtil.f215m);
                        stringBuffer.append(str);
                        stringBuffer.append(onlTable.getMainField());
                    }
                }
                if (!z4) {
                    stringBuffer2.append(m562a);
                    if (str4.length() > 0) {
                        if (z) {
                            sb.append(str4);
                            z = false;
                        } else {
                            sb.append(" ").append(m327c.getValue()).append(" ").append(str4);
                        }
                    }
                }
                String str5 = tableName + ".";
                while (stringBuffer2.toString().toUpperCase().contains(str5.toUpperCase())) {
                    int indexOf = stringBuffer2.toString().toUpperCase().indexOf(str5.toUpperCase());
                    stringBuffer2.replace(indexOf, indexOf + tableName.length(), alias);
                }
            }
        }
        String m165a = m165a(arrayList, hashMap, hashMap2);
        String m170a = m170a(sb);
        String m167a = m167a(arrayList2);
        String str6 = CgformUtil.SELECT + m165a + stringBuffer.toString() + CgformUtil.f210h + stringBuffer2.toString() + m170a;
        if (!DbTypeUtils.dbTypeIsSqlServer(m27c)) {
            str6 = str6 + m167a;
        }
        f462a.info("---Online联合查询sql :>> " + str6);
        f462a.info("---Online联合查询sqlParams :>> " + hashMap3);
        OnlQueryModel onlQueryModel = new OnlQueryModel(str6, hashMap3);
        onlQueryModel.setTableAliasMap(hashMap2);
        for (OnlTable onlTable2 : m161a) {
            List<OnlCgformField> selectFieldList2 = onlTable2.getSelectFieldList();
            if (isNewExport) {
                for (OnlCgformField onlCgformField : selectFieldList2) {
                    String dbFieldName = onlCgformField.getDbFieldName();
                    Integer num = hashMap.get(dbFieldName);
                    if (num != null && num.intValue() > 1 && !onlTable2.m266a()) {
                        onlCgformField.setDbFieldName(onlTable2.getTableName() + "_" + dbFieldName);
                    }
                    arrayList3.add(onlCgformField);
                }
            } else if (ignoreSelectSubField && onlTable2.m266a()) {
                arrayList3 = selectFieldList2;
            }
        }
        onlQueryModel.setFieldList(arrayList3);
        return onlQueryModel;
    }

    @Override // org.jeecg.modules.online.cgform.service.IOnlineJoinQueryService
    public XSSFWorkbook handleOnlineExport(OnlCgformHead head, Map<String, Object> params) {
        OnlQueryModel queryInfo;
        List arrayList;
        XSSFWorkbook xSSFWorkbook = new XSSFWorkbook();
        boolean m349a = CgformUtil.m349a(head);
        if (m349a) {
            queryInfo = getQueryInfo(head, params, false, true);
        } else {
            queryInfo = this.onlCgformFieldService.getQueryInfo(head, params, null);
        }
        boolean z = true;
        Integer num = 50000;
        Integer num2 = 1;
        String sql = queryInfo.getSql();
        Map<String, Object> params2 = queryInfo.getParams();
        List<OnlCgformField> fieldList = queryInfo.getFieldList();
        List<ExcelExportEntity> m337b = CgformUtil.m337b(fieldList, "id", this.upLoadPath);
        boolean z2 = false;
        while (z) {
            Page<Map<String, Object>> page = new Page<>(num2.intValue(), num.intValue());
            page.setOptimizeCountSql(false);
            page.setSearchCount(false);
            Integer num3 = num2;
            num2 = Integer.valueOf(num2.intValue() + 1);
            params.put("pageNo", num3);
            f462a.info("---Online表单导出-查询sql: >>  " + sql);
            f462a.info("---Online表单导出-查询sqlParam: >>  " + params2.toString());
            List<Map<String, Object>> m322d = CgformUtil.m322d(this.onlineMapper.selectPageByCondition(page, sql, params2).getRecords());
            if (m322d == null || m322d.size() == 0) {
                z = false;
            } else {
                ArrayList arrayList2 = new ArrayList();
                String obj = params.get("selections") == null ? null : params.get("selections").toString();
                if (oConvertUtils.isNotEmpty(obj)) {
                    z = false;
                    if (m349a) {
                        Map<String, List<String>> m316f = CgformUtil.m316f(obj, new ArrayList(queryInfo.getTableAliasMap().values()));
                        arrayList = (List) m322d.stream().filter(map -> {
                            return m164a(map, m316f);
                        }).collect(Collectors.toList());
                    } else {
                        List<String> m312h = CgformUtil.m312h(obj);
                        arrayList = (List) m322d.stream().filter(map2 -> {
                            return m312h.contains(map2.get("id"));
                        }).collect(Collectors.toList());
                    }
                } else {
                    List arrayList3 = m322d;
                    if (m322d == null) {
                        arrayList3 = new ArrayList();
                    }
                    arrayList2.addAll(arrayList3);
                    arrayList = arrayList2;
                }
                ConvertUtil.m398a(1, arrayList, fieldList);
                try {
                    this.onlCgformHeadService.executeEnhanceExport(head, arrayList);
                } catch (BusinessException e) {
                    f462a.error("导出java增强处理出错", e.getMessage());
                }
                if (head.getTableType().intValue() == 2 && !m349a && oConvertUtils.isEmpty(params.get(CgformUtil.f277aw))) {
                    String subTableStr = head.getSubTableStr();
                    if (oConvertUtils.isNotEmpty(subTableStr)) {
                        for (String str : subTableStr.split(CgformUtil.f233E)) {
                            addAllSubTableDate(str, params, arrayList, m337b, z2);
                        }
                        z2 = true;
                    }
                }
                ExcelExportServer excelExportServer = new ExcelExportServer();
                ExportParams exportParams = new ExportParams();
                exportParams.setType(ExcelType.XSSF);
                excelExportServer.createSheetForMap(xSSFWorkbook, exportParams, m337b, arrayList);
            }
        }
        return xSSFWorkbook;
    }

    @Override // org.jeecg.modules.online.cgform.service.IOnlineJoinQueryService
    public void addAllSubTableDate(String subTable, Map<String, Object> params, List<Map<String, Object>> result, List<ExcelExportEntity> entityList, boolean subEntityExist) {
        if (oConvertUtils.isEmpty(subTable)) {
            return;
        }
        OnlCgformHead onlCgformHead = this.onlCgformHeadService.getOne( new LambdaQueryWrapper<OnlCgformHead>().eq(OnlCgformHead::getTableName, subTable));
        LambdaQueryWrapper<OnlCgformField> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(OnlCgformField::getCgformHeadId, onlCgformHead.getId());
        lambdaQueryWrapper.orderByAsc(OnlCgformField::getOrderNum);
        List list = this.onlCgformFieldService.list(lambdaQueryWrapper);
        String str = "";
        String str2 = "";
        Iterator it = list.iterator();
        while (true) {
            if (!it.hasNext()) {
                break;
            }
            OnlCgformField onlCgformField = (OnlCgformField) it.next();
            if (oConvertUtils.isNotEmpty(onlCgformField.getMainField())) {
                str = onlCgformField.getMainField();
                str2 = onlCgformField.getDbFieldName();
                break;
            }
        }
        if (!subEntityExist) {
            ExcelExportEntity excelExportEntity = new ExcelExportEntity(onlCgformHead.getTableTxt(), subTable);
            excelExportEntity.setList(CgformUtil.m337b(list, "id", this.upLoadPath));
            entityList.add(excelExportEntity);
        }
        for (int i = 0; i < result.size(); i++) {
            params.put(str2, result.get(i).get(str));
            String m370a = CgformUtil.m370a(onlCgformHead.getTableName(), list, params);
            f462a.info("-----------动态列表查询子表sql》》" + m370a);
            List<Map<String, Object>> queryListData = this.onlCgformHeadService.queryListData(m370a);
            ConvertUtil.m398a(1, queryListData, list);
            result.get(i).put(subTable, CgformUtil.m322d(queryListData));
        }
    }

    /* renamed from: a */
    private boolean m164a(Map<String, Object> map, Map<String, List<String>> map2) {
        boolean z = true;
        for (String str : map2.keySet()) {
            z = z && map2.get(str).contains(map.get(str));
        }
        return z;
    }
}
